<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/3/27
 * Time: 21:06
 */
namespace app\admin\model\frontend;

use app\admin\helper\DBhelper;
use app\admin\model\BaseM;
use app\admin\model\LogsM;
use think\Config;
use think\Request;
use think\Session;

class IndexM extends  BaseM
{
    protected $table = 'tb_index';
    protected $module = '主页配置';

    /*
     * rule
     * 验证data
     * @params $data
     * 新增和修改时验证
     * */
    public function validate_model(){

    }

    /*
      * 列表
      * */
    public function listData()
    {
        $data  = $this->find();
        if (Request::instance()->isGet()) {
            $log = [
                'op_uid' => Session::get('username'),
                'op_module' => $this->module,
                'op_type' => 'search',
                'op_origin' => $this->table,
                'op_data' => 'find one',
                'op_ip' => getIP(),
                'op_sql' => $this->getLastSql(),
            ];
            LogsM::add($log);
            pr_log($log,'operate');
        }
        return $data;
    }

    /**
     * save & update
     * @return array|mixed
     */
    public function config(){
        $data = input('post.');
        $data['username'] = Session::get('username');
        $id = $data['id'];
        if (empty($id) && !empty($data)){
            //新增
            $data['created_at']=time();
            $data['updated_at']=time();
            $re = $this->save($data);
            $last_sql = Config::get('app_debug')?$this->getLastSql():'';
            if($re){
                $log = [
                    'op_uid' => Session::get('username'),
                    'op_module' => $this->module,
                    'op_type' => 'add',
                    'op_origin' => $this->table,
                    'op_data' => json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
                    'op_ip' => getIP(),
                    'op_sql' => $this->getLastSql(),
                ];
                LogsM::add($log);
                pr_log($log,'operate');
                return op_success($last_sql);
            }
            return op_fail($last_sql);
        }else {
            //只修改变化字段
            $origin = $this->getById($id)->toArray();
            $data = DBhelper::getChangeData($data,$origin);
            if (!empty($data)){
                $data['updated_at']=time();
                $re = $this->where('id',$id)->update($data);
                $last_sql = Config::get('app_debug')?$this->getLastSql():'';
                if($re){
                    $log = [
                        'op_uid' => Session::get('username'),
                        'op_module' => $this->module,
                        'op_type' => 'edit',
                        'op_origin' => $this->table,
                        'op_data' => json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
                        'op_ip' => getIP(),
                        'op_sql' => $this->getLastSql(),
                    ];
                    LogsM::add($log);
                    pr_log($log,'operate');
                    return op_success($last_sql);
                }
                return op_fail($last_sql);
            }
        }
        return op_nochange();
    }

}